package com.example.lotterysystem.service.activitystatus.operator;

import com.example.lotterysystem.dao.dataobject.ActivityDO;
import com.example.lotterysystem.dao.mapper.ActivityMapper;
import com.example.lotterysystem.dao.mapper.PrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.example.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Spider-Man
 * Date: 2025-09-15
 * Time: 11:13
 */
@Component
public class ActivityOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityMapper activityMapper; //获取到获得参数当中的活动信息
    @Autowired
    private PrizeMapper prizeMapper; //需要查看奖品是否被抽取完
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityStatus();
        //需要判断奖品是否全部抽取完成
        if(null == activityId || null == targetStatus) {
            //活动不存在,或者活动目标状态为空则表示不需要扭转
            return false;
        }
        //需要扭转
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if(activityId == null) {
            return false; //活动不存在
        }
        //活动存在
        if(activityDO.getStatus().equalsIgnoreCase(targetStatus.name())) {
            //当前活动状态一致,不需要进行扭转
            return false;
        }
        //奖品是否全部抽完
        //
        int count = prizeMapper.countPrize(activityId,ActivityPrizeStatusEnum.INIT.name());
        if(count > 0) {
            return false;//奖品还没抽完
        }
        return true;
    }

    /**
     * 进行状态转换
     */
    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        } catch (Exception e) {
            return false;
        }

    }

}
